import Data.List
import Control.Monad
import Control.Applicative
import System.IO

type Customer = (Maybe Int, [Int])

main = do
  putStrLn $ show $ s 1 2
  putStrLn $ show $ s 0 2
  putStrLn $ show $ s 1 0  
  -- return $ readCustomer $ words "2 1 0 2 0"
  -- return $ readCustomer $ words "2 1 0 2 0 5 1 3 0"
  -- return $ readCustomer $ words "2 1 0 2 0 5 1 3 0 4 1" -- fatal
  nCases <- read <$> getLine
  mapM_ processCase [1..nCases]
  where s a 0 = a*10
        s 0 b = b*100
        s a b = a+b

processCase :: Int -> IO ()
processCase i = do
  nShakes <- read <$> getLine :: IO Int
  nCustomers <- read <$> getLine :: IO Int
  customerLines <- replicateM nCustomers getLine
  let customers = readCustomers customerLines
  -- putStrLn $ show customerLines
  -- putStrLn $ show $ readCustomers customerLines
  putStrLn $ "Case #" ++ (show i) ++ ": " ++ (show customers)

readCustomers :: [String] -> [Customer]
readCustomers = map (readCustomer . words)

readCustomer :: [String] -> Customer
readCustomer (h:hs) =
  readCustomer2 (Nothing, []) hs

readCustomer2 :: Customer -> [String] -> Customer
readCustomer2 a [] = a
readCustomer2 a (x:y:xs) = readCustomer3 a (read x) (read y) xs

readCustomer3 :: Customer -> Int -> Int -> [String] -> Customer
readCustomer3 (malted, unmalted) flavor 0 xs = readCustomer2 (malted, flavor:unmalted) xs
readCustomer3 (Nothing, unmalted) flavor 1 xs = readCustomer2 (Just flavor, unmalted) xs

-- import Data.List
import Control.Monad
import Control.Applicative
import System.IO

type Customer = (Maybe Int, [Int])

main = do
  putStrLn $ show $ s 1 2
  putStrLn $ show $ s 0 2
  putStrLn $ show $ s 1 0  
  -- return $ readCustomer $ words "2 1 0 2 0"
  -- return $ readCustomer $ words "2 1 0 2 0 5 1 3 0"
  -- return $ readCustomer $ words "2 1 0 2 0 5 1 3 0 4 1" -- fatal
  nCases <- read <$> getLine
  mapM_ processCase [1..nCases]
  where s a 0 = a*10
        s 0 b = b*100
        s a b = a+b

processCase :: Int -> IO ()
processCase i = do
  nShakes <- read <$> getLine :: IO Int
  nCustomers <- read <$> getLine :: IO Int
  customerLines <- replicateM nCustomers getLine
  let customers = readCustomers customerLines
  -- putStrLn $ show customerLines
  -- putStrLn $ show $ readCustomers customerLines
  putStrLn $ "Case #" ++ (show i) ++ ": " ++ (show customers)

readCustomers :: [String] -> [Customer]
readCustomers = map (readCustomer . words)

readCustomer :: [String] -> Customer
readCustomer (h:hs) =
  readCustomer2 (Nothing, []) hs

readCustomer2 :: Customer -> [String] -> Customer
readCustomer2 a [] = a
readCustomer2 a (x:y:xs) = readCustomer3 a (read x) (read y) xs

readCustomer3 :: Customer -> Int -> Int -> [String] -> Customer
readCustomer3 (malted, unmalted) flavor 0 xs = readCustomer2 (malted, flavor:unmalted) xs
readCustomer3 (Nothing, unmalted) flavor 1 xs = readCustomer2 (Just flavor, unmalted) xs

-- Get list of malted shakes for list of customers
solveCase :: [Customer] -> Maybe [Int]
solveCase = foldM solver (Just [])
  where solver malted (Nothing, [good]) = 

-- solver :: [Int] -> Customer -> [Int] -- solution[N-1] -> Customer -> solution[N]